package com.zhiwei.codesecurity.common.webservice.checkmarx.wsdk.utils;

import com.zhiwei.codesecurity.common.utils.StringUtils;
import com.zhiwei.codesecurity.common.webservice.checkmarx.v7.*;
import com.zhiwei.codesecurity.common.webservice.checkmarx.wsdk.common.ReportType;
import com.zhiwei.codesecurity.common.webservice.checkmarx.wsdk.common.TaskResult;
import com.zhiwei.codesecurity.common.enums.TaskStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Calendar;
import java.util.Date;

public class SDKUtil {
    private static Logger logger = LoggerFactory.getLogger(SDKUtil.class);

    public static TaskResult parseCxWSResponseScanStatus(CxWSResponseScanStatus cxWSResponseScanStatus) {
        if (null == cxWSResponseScanStatus) {
            return null;
        }
        TaskResult taskResult = new TaskResult();
        taskResult.setSuccesfull(cxWSResponseScanStatus.isIsSuccesfull());
        taskResult.setErrorMessage(cxWSResponseScanStatus.getErrorMessage());
        taskResult.setSubmitRunId(StringUtils.isBlank(cxWSResponseScanStatus.getRunId()) ? -1L :
                Long.parseLong(cxWSResponseScanStatus.getRunId()));
        taskResult.setCurrentStage(cxWSResponseScanStatus.getCurrentStage());
        taskResult.setQueuePosition(cxWSResponseScanStatus.getQueuePosition());
        taskResult.setTotalPercent(cxWSResponseScanStatus.getTotalPercent());
        taskResult.setCurrentStagePercent(cxWSResponseScanStatus.getCurrentStagePercent());
        taskResult.setOwner(cxWSResponseScanStatus.getOwner());
        taskResult.setStageName(cxWSResponseScanStatus.getStageName());
        taskResult.setStageMessage(cxWSResponseScanStatus.getStageMessage());
        taskResult.setStepMessage(cxWSResponseScanStatus.getStepMessage());
        taskResult.setStepDetails(cxWSResponseScanStatus.getStepDetails());
        taskResult.setResultId(cxWSResponseScanStatus.getResultId());
        taskResult.setScanId(cxWSResponseScanStatus.getScanId());
        taskResult.setProjectId(cxWSResponseScanStatus.getProjectId());
        taskResult.setTaskId(cxWSResponseScanStatus.getTaskId());
        taskResult.setTaskName(cxWSResponseScanStatus.getTaskName());
        taskResult.setProjectName(cxWSResponseScanStatus.getProjectName());
        taskResult.setRunId(StringUtils.isBlank(cxWSResponseScanStatus.getRunId()) ? "-1" :
                cxWSResponseScanStatus.getRunId());
        taskResult.setSourceId(cxWSResponseScanStatus.getSourceId());
        taskResult.setLoc(cxWSResponseScanStatus.getLOC());
        taskResult.setTimeScheduled(parseCxDateTime(cxWSResponseScanStatus.getTimeScheduled()));
        taskResult.setElapsedTime(parseCxDateTime(cxWSResponseScanStatus.getElapsedTime()));
        taskResult.setTimeFinished(parseCxDateTime(cxWSResponseScanStatus.getTimeFinished()));
        taskResult.setTimeBeginWorking(parseCxDateTime(cxWSResponseScanStatus.getTimeBeginWorking()));
        taskResult.setCurrentStatus(parseCurrentStatus(cxWSResponseScanStatus.getCurrentStatus()));
        taskResult.setServerName(cxWSResponseScanStatus.getServerName());
        taskResult.setOrigin(cxWSResponseScanStatus.getOrigin());
        taskResult.setPublic(cxWSResponseScanStatus.isIsPublic());
        taskResult.setPartialResults(cxWSResponseScanStatus.isPartialResults());

        return taskResult;
    }

    public static Date parseCxDateTime(CxDateTime cd) {
        if (cd == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.set(cd.getYear(), cd.getMonth(), cd.getDay(), cd.getHour(), cd.getMinute(), cd.getSecond());
        return c.getTime();
    }

    private static TaskStatus parseCurrentStatus(CurrentStatusEnum currentStatusEnum) {
        TaskStatus taskStatus;
        switch (currentStatusEnum) {
            case CANCELED:
                taskStatus = TaskStatus.CANCELED;
                break;
            case DELETED:
                taskStatus = TaskStatus.DELETED;
                break;
            case FAILED:
                taskStatus = TaskStatus.FAILED;
                break;
            case FINISHED:
                taskStatus = TaskStatus.FINISHED;
                break;
            case QUEUED:
                taskStatus = TaskStatus.QUEUED;
                break;
            case UNZIPPING:
                taskStatus = TaskStatus.UNZIPPING;
                break;
            case WAITING_TO_PROCESS:
                taskStatus = TaskStatus.WAITING_TO_PROCESS;
                break;
            case WORKING:
                taskStatus = TaskStatus.WORKING;
                break;
            default:
                taskStatus = TaskStatus.UNKNOWN;
                break;
        }
        return taskStatus;
    }

    public static CxWSReportType parseReportType(ReportType reportType) {
        CxWSReportType cxWSReportType;
        switch (reportType) {
            case CSV:
                cxWSReportType = CxWSReportType.CSV;
                break;
            case XML:
                cxWSReportType = CxWSReportType.XML;
                break;
            case PDF:
                cxWSReportType = CxWSReportType.PDF;
                break;
            case RTF:
                cxWSReportType = CxWSReportType.RTF;
                break;
            default:
                cxWSReportType = CxWSReportType.PDF;
                break;
        }
        return cxWSReportType;
    }

    /**
     * 根据组名查找组ID
     * @param groups
     * @param groupName
     * @return
     */
    public static String getAssociatedGroupIDByName(Group[] groups, String groupName) {
        if (null == groups || groups.length == 0 || StringUtils.isBlank(groupName)) {
            return "";
        }

        String groupId = "";
        for (int i = 0; i < groups.length; i++) {
             if (groups[i].getGroupName().equals(groupName)) {
                 groupId = groups[i].getID();
                 break;
             }
        }
        return groupId;
    }

    /**
     * 根据presetName获取presetID
     * @param presets
     * @param presetName
     * @return
     */
    public static long getPresetIDByName(Preset[] presets, String presetName) {
        if (null == presets || presets.length == 0 || StringUtils.isBlank(presetName)) {
            return -1L;
        }

        long presetId = -1L;
        for (int i = 0; i < presets.length; i++) {
            if (presets[i].getPresetName().equals(presetName)) {
                presetId = presets[i].getID();
                break;
            }
        }
        return presetId;
    }

    public static long getScanConfigurationIDByName(ConfigurationSet[] configurationSets, String configSetName) {
        if (null == configurationSets || configurationSets.length == 0 || StringUtils.isBlank(configSetName)) {
            return -1L;
        }

        long configSetId = -1L;
        for(int i = 0; i < configurationSets.length; i++) {
            if (configurationSets[i].getConfigSetName().equals(configSetName)) {
                configSetId = configurationSets[i].getID();
                break;
            }
        }
        return configSetId;
    }
}
